
function removeItem(btn){
    const itemId = $(btn).parent().parent().siblings('.product-qty').children('.itemId').val()
    console.log(itemId)
    const originTotal = parseFloat($(btn).parent().parent().siblings('.tmp-total').children('.c_total').text())
    const subTotal = parseFloat($('#subtotal').text())
    $('#subtotal').text((subTotal-originTotal).toFixed(2))
    $(btn).parent().parent().parent().remove()
    $.post("/shop/cart/del/"+itemId,function(data,status){
        // alert("数据: " + JSON.stringify(data) + "\n状态: " + status);
        console.log("数据: " + JSON.stringify(data) + "\n状态: " + status);
    });
    console.log('cartTmp ready')
    $.ajax({
        type: 'get',
        url: "/cart/checkAmount",
        async: true,
        success: function (result) {
            console.log('check cart amount: ' + result)
            if (result > 0) {
                // $('#cartTmp').after("<span class=\"dot dot-md bg-success\"></span>")
                $('#cartTmp').css('display','inline-block')
            }
        }
    });
}

$(".increase.items-count").click(function(){
    const itemId = $(this).parent().parent().children('.itemId').val()
    console.log("itemId")
    console.log(itemId)
    $.post("/shop/cart/add/"+itemId,function(data,status){
        // alert("数据: " + JSON.stringify(data) + "\n状态: " + status);
        console.log("increase数据: " + JSON.stringify(data) + "\n状态: " + status);
    });
});

//reduced多个d
$(".reduced.items-count").click(function(){
    if (parseInt($(this).siblings('input').val())>1){
        const itemId = $(this).parent().parent().children('.itemId').val()
        console.log("itemId")
        console.log(itemId)
        $.post("/shop/cart/cut/"+itemId,function(data,status){
            // alert("数据: " + JSON.stringify(data) + "\n状态: " + status);
            console.log("reduced数据: " + JSON.stringify(data) + "\n状态: " + status);
        });
    }else {
        console.log('不能再减少了')
    }
});




/*-------------------
	  Handle Total Price
	        处理总价
	--------------------- */

function setSubTotal(input) {
    //遍历所有单商品total，读，更新subtotal
    let allTmpTotal = input.parents('.table').find('.c_total')
    console.log(allTmpTotal)
    // console.log(allTmpTotal.text())
    let subTotal = input.parents('.product-single').siblings('.product-subtotal').find('#subtotal')
    console.log(subTotal)
    let tmpTotal = 0.00
    let idx = 0;
    while (idx < allTmpTotal.length){
        let tmp = allTmpTotal[idx]
        console.log(tmp.innerText)
        tmpTotal += parseFloat(tmp.innerText)
        idx++
    }
    console.log(tmpTotal)
    //特判
    if (!isNaN(tmpTotal) && tmpTotal<0){
        subTotal.text("")
    }else {
        subTotal.text(tmpTotal.toFixed(2))
    }
}

function setOneTotal(node,input){
    //对应单价节点
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    // let sTmp = node.parent().parent().prev().children('.member_price').text()
    let temp = node.parent().parent().prev().children('.member_price')
    if (temp.length===0){
        temp = node.parent().parent().prev().children('.c_price')
    }
    let sTmp = temp.text()
    let single = parseFloat(sTmp)
    //对应总价节点
    let tolTmp = node.parent().parent().next().children('.c_total')
    //改总价：
    let price = parseFloat(input.val())*single
    //特判
    if (!isNaN(price) && price<0){
        tolTmp.text("")
    }else {
        tolTmp.text(price.toFixed(2))
    }
    return false
}

function initTotals() {
    console.log("INIT")
    //用索引对应关系 不用相对位置了
    let totals = $('.c_total')
    console.log(totals)
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    let temp = $('.c_price')
    if (temp.length === 0){
        temp = $('.member_price')
    }
    let prices = temp
    // let prices = $('.member_price')
    console.log(prices)
    let counts = $('.input-text.qty')
    console.log(counts)
    for (let i = 0; i<totals.length; ++i){
        console.log(i)
        let price = parseFloat(prices[i].innerText)
        totals[i].innerText = (parseInt(counts[i].value)*price).toFixed(2)
    }
}

function test(){
    console.log("AAAA")
}

<!-- 改变购物车物品数量 -->
$(function(){

    //init
    $(document).ready(function () {
        test()
        initTotals()
        setSubTotal($('.input-text.qty'))
    })

    //increase
    $('.increase.items-count').click(function () {
        //找<input>
        let input = $(this).siblings("input");
        let count = input.val();
        // console.log('+'+count)
        if (!isNaN(count)){
            input.val(parseInt(count)+1);
            console.log(count+"++")
            setOneTotal($(this),input)
            setSubTotal(input)
        }
        return false;
    })
    //reduce
    $('.reduced.items-count').click(function () {
        //找<input>
        let input = $(this).siblings("input");
        let count = input.val();
        // console.log('-'+count)
        if (!isNaN(count) && parseFloat(count)>1){
            input.val(parseFloat(count)-1);
            console.log("count--: "+count)
            setOneTotal($(this),input)
            setSubTotal(input)
        }
        return false;
    })
    //oneTotal
    $('.qty').on('input propertychange', function() {
        console.log('TEST')
        //获取input 元素,并实时监听用户输入
        let input = $(this)
        setOneTotal($(this),input)
        setSubTotal(input)
    })
});
